package com.itheima.sc.web.order;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.itheima.sc.pojo.order.OmsOrder;
import com.itheima.sc.result.Result;
import com.itheima.sc.service.order.OmsOrderService;
import com.itheima.sc.service.order.OmsOrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OmsOrderController {
    @Autowired
    OmsOrderService omsOrderService;
    @Autowired
    OmsOrderUtil omsOrderUtil;

    @GetMapping("list")
    public Result list(
            @RequestParam Map map
    ) {
        Integer pageNum = Convert.toInt(map.get("pageNum"));
        Integer pageSize = Convert.toInt(map.get("pageSize"));

        return omsOrderService.listOmsOrder(pageNum, pageSize, map);
    }

    @PostMapping("delete")
    public Result deletOrders(@RequestParam Map<String, String> map) {
        String ids = map.get("ids");
        String[] split = ids.split(",");
        ArrayList<Long> idss = new ArrayList<>();
        for (String s : split) {
            idss.add(Convert.toLong(s));
        }
        return omsOrderService.deletByIds(idss);

    }

    /**
     * 关闭订单
     */
    @PostMapping("update/close")
    public Result closeOrder(@RequestParam Map<String, String> map) {
        String ids = map.get("ids");
        String[] split = ids.split(",");
        ArrayList<Long> idss = new ArrayList<>();
        for (String s : split) {
            idss.add(Convert.toLong(s));
        }
        String note = map.get("note");
        return omsOrderService.closeOrderByIds(idss, note);
    }

    /**
     * 批量发货
     */
    @PostMapping("update/delivery")
    public Result deliveryOrder(@RequestBody ArrayList<Map<String, String>> mapArrayList) {
        for (Map<String, String> map : mapArrayList) {
            Long id = Convert.toLong(map.get("orderId"));
            omsOrderService.deliveryOrderByIds(id, map);
        }
        return Result.success("发货成功");
    }

    //修改订单费用信息
    @PostMapping("update/moneyInfo")
    public Result moneyInfo(@RequestBody Map<String, String> moneyInfoParam) {
        omsOrderUtil.saveHistoryOpe(CollUtil.toList(
                Convert.toLong(moneyInfoParam.get("orderId"))),
                0,
                "修改费用信息");

        return omsOrderService.updateMoneyInfo(moneyInfoParam);

    }

    //订单备注
    @PostMapping("update/note")
    public Result note(@RequestParam Map<String, String> map) {
        //保存订单修改记录
        Integer status = Convert.toInt(map.get("status"));
        //保存操作记录。
        omsOrderUtil.saveHistoryOpe(
                CollUtil.toList(Convert.toLong(map.get("id"))),
                Convert.toInt(map.get("status")),
                "修改订单备注");
        omsOrderService.noteById(map);
        return Result.success("备注修改成功");
    }

    //修改收货人信息
    @PostMapping("update/receiverInfo")
    public Result receiverInfo(@RequestBody Map<String, String> map) {
        //保存修改收货地址的操作记录
        omsOrderUtil.saveHistoryOpe(CollUtil.toList(Convert.toLong(
                map.get("orderId"))),
                Convert.toInt(map.get("status")),
                "修改收货人信息");

        return omsOrderService.receiverInfoById(map);
    }

    //获取订单详情：返回历史记录和商品列表
    @GetMapping("{id}")
    public Result orderById(@PathVariable Long id) {
// 订单详情
        OmsOrder omsOrder = omsOrderService.orderInfoById(id);
        return Result.success(omsOrder);
    }
}
